#    Copyright 2012 New Mexico EPSCoR
#
#    This file is part of Teacher's P.E.T.
#
#    Teacher's P.E.T. is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Teacher's P.E.T. is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Teacher's P.E.T..  If not, see <http://www.gnu.org/licenses/>.
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from django import forms
from models import FAQ, FAQForm

class MultiorderChangeList(ChangeList):
	def get_query_set(self):
		queryset = super(MultiorderChangeList,self).get_query_set()
		if self.model_admin.ordering:
			queryset = queryset.order_by(*self.model_admin.ordering)
		return queryset
	
class FAQAdmin(admin.ModelAdmin):
	form = FAQForm
	list_display = ['category', 'question_id','question','question_link','for_admin']
	list_display_links = ['question']
	list_filter = ['for_admin', 'category']
	list_editable = ['category', 'question_id', 'question_link']
	ordering = ['category','question_id']
	
	def get_changelist(self, request, **kwargs):
		return MultiorderChangeList
	def formfield_for_dbfield(self, db_field, **kwargs):
		field = super(FAQAdmin, self).formfield_for_dbfield(db_field, **kwargs)
		if db_field.name == 'question':
			field.widget.attrs['style'] = "width: 100%; " + field.widget.attrs.get('style', '')
		elif db_field.name == 'category':
			oldattrs = field.widget.attrs
			field.widget = forms.TextInput(attrs=oldattrs)
			field.widget.attrs['style'] = "width: 100%; " + field.widget.attrs.get('style', '')
			field.widget.attrs['size'] = "100"
		elif db_field.name == 'answer':
			field.widget.attrs['style'] = "width: 100%; " + field.widget.attrs.get('style', '')
			field.widget.attrs['rows'] = 75
		return field
		
admin.site.register(FAQ, FAQAdmin)
